package com.example.mynotepad.adaper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.mynotepad.bean.RecordBean;
import com.example.mynotepad.R;


import java.util.List;

public class NotepadAdapter extends BaseAdapter {

    private Context context;
    private List<RecordBean> recordBeanList;

    public NotepadAdapter(Context context, List<RecordBean> recordBeanList) {
        this.context = context;
        this.recordBeanList = recordBeanList;
    }

    @Override
    public int getCount() {
        // 三目运算符
        return recordBeanList == null ? 0 : recordBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return recordBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // 可复用记录的方法
        ViewHolder viewHolder;
        if (view == null){
            // 新条目
            view = LayoutInflater.from(context)
                    .inflate(R.layout.notepad_item_layout, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            // 可复用的旧条目
            viewHolder = (ViewHolder) view.getTag();
        }
        // 取集合中条目内容
        RecordBean recordBean = recordBeanList.get(i);
        // 赋值给条目控件
        viewHolder.tvRecord.setText(recordBean.getRecord());
        viewHolder.tvTime.setText(recordBean.getTime());
        return view;
    }

    class ViewHolder{
        TextView tvRecord;
        TextView tvTime;

        public ViewHolder(View view) {
            tvRecord = view.findViewById(R.id.item_content);
            tvTime = view.findViewById(R.id.item_time);
        }
    }
}

